<?php

include_once APPLICATION_DIR . "/application/model/BANNER.php";

class Banner extends DataObjects_BANNER
{
	public function validateInput(){
        global $error_msg;

		$posted_file = $_FILES["IMAGE"];
		if ($error_msg->isNoError() && !empty($posted_file["name"])) {
			if ($this->ID) {
				$this->OLD_IMAGE = $this->IMAGE;
			}
            $upload = new UPLOAD();
			$new_file = $upload->doUploadImage(IMAGE_DIR."/", 'IMAGE', '', '190', '');
			if ($new_file) {
				$this->IMAGE = $new_file;
			} else {
				$error_msg->add('_MSG_UPLOAD_FAILED');
			}
		}

		return $error_msg->isNoError();
    }

	public function update() {
		if (!empty($this->OLD_IMAGE) && $this->IMAGE != $this->OLD_IMAGE) {
			@unlink(IMAGE_DIR."/{$this->OLD_IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->OLD_IMAGE}");
		}

		return parent::update();
	}

	public function delete() {
		$banner_module = new Banner_Module();
		$banner_module->ID_BANNER = $this->ID;
		$banner_module->find();
		while ($banner_module->fetch()) {
			$banner_module->delete();
		}

		if ($this->IMAGE) {
			@unlink(IMAGE_DIR."/{$this->IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->IMAGE}");
		}

		return parent::delete();
	}

	public static function updateStatus($id, $status) {
		$banner = new Banner();

		if ($banner->get($id)) {
			$banner->DISABLED = $status;
			if ($banner->update()) {
				return 1;
			}
		}
		return 0;
	}
    public function getBanner($cont, $action){
        $sql="SELECT b.* FROM BANNER b
                INNER JOIN BANNER_MODULE bm ON bm.ID_BANNER = b.ID
                INNER JOIN MODULE m ON m.ID = bm.ID_MODULE
                WHERE b.IMAGE <> '' AND b.DISABLED = 0 
                    AND m.MOD = '".$cont."' AND m.ACTION = '".$action."'
                GROUP BY b.ID";
        $this->query($sql);
    }
}
?>
